package com.cssw.fyzb.web.controller;

import com.alibaba.fastjson.JSON;
import com.cssw.fyzb.base.web.context.UserContext;
import com.cssw.fyzb.base.web.domain.AjaxData;
import com.cssw.fyzb.base.web.domain.AjaxPage;
import com.cssw.fyzb.base.web.domain.AjaxResult;
import com.cssw.fyzb.web.dto.OperIdDto;
import com.cssw.fyzb.web.dto.SaveOperDto;
import com.cssw.fyzb.web.dto.SaveOperRoleModel;
import com.cssw.fyzb.web.dto.UpdateOperDto;
import com.cssw.fyzb.web.service.OperService;
import com.cssw.fyzb.web.util.SplitUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Feng Chen
 */
@ApiModel("账号接口接口")
@RestController
@RequestMapping("/oper")
public class OperController {

    @Autowired
    private OperService operService;

    @ApiOperation(value = "分页查询账号记录", response = AjaxPage.class)
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "mobileNo", value = "电话号码", paramType = "query", dataType = "string", required = false),
            @ApiImplicitParam(name = "operCode", value = "工号", paramType = "query", dataType = "string", required = false),
            @ApiImplicitParam(name = "operName", value = "姓名", paramType = "query", dataType = "string", required = false),
            @ApiImplicitParam(name = "idCard", value = "身份证号码", paramType = "query", dataType = "string", required = false)
    })
    public AjaxPage list(int page, int limit, String mobileNo, String operCode, String operName, String idCard) throws Exception {
        Map param = new HashMap<>();
        param.put("mobileNo", mobileNo);
        param.put("operCode", operCode);
        param.put("operName", operName);
        param.put("idCard", idCard);
        return operService.list(page, limit, param);
    }

    @ApiOperation(value = "分页查询账号记录", response = AjaxPage.class)
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public AjaxData info(Long operId) {
        return operService.info(operId);
    }

    @ApiOperation(value = "新增账号", response = AjaxData.class)
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxData<Object> save(@RequestBody SaveOperDto dto, BindingResult result) {
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            ObjectError fieldError = errors.get(0);
            return AjaxData.failure(fieldError.getDefaultMessage());
        }
        return operService.save(dto.getOperCode(), dto.getOperPwd(), dto.getMobileNo(), dto.getOperName(), "1", dto.getRemark(), UserContext.getUserId());
    }

    @ApiOperation(value = "修改账号", response = AjaxData.class)
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public AjaxData update(@RequestBody UpdateOperDto dto, BindingResult result) {
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            ObjectError fieldError = errors.get(0);
            return AjaxData.failure(fieldError.getDefaultMessage());
        }
        return operService.update(dto.getOperId(), dto.getOperCode(), dto.getOperPwd(), dto.getMobileNo(), dto.getOperName(), "1", dto.getRemark(), UserContext.getUserId());
    }

//    @ApiOperation(value = "删除账号", response = AjaxData.class)
//    @RequestMapping(value = "/delete", method = RequestMethod.POST)
//    public AjaxData delete(@Valid OperIdDto dto, BindingResult result) throws Exception {
//        List<Long> operIds = SplitUtil.splitToLongList(dto.getOperId());
//        return operService.delete(operIds, UserContext.getUserId());
//    }

    @ApiOperation(value = "删除账号", response = AjaxData.class)
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public AjaxData delete(@RequestBody Map<String, Long> dto) {
        return operService.delete(dto.get("operId"), UserContext.getUserId());
    }

    @ApiOperation(value = "启用账号", response = AjaxData.class)
    @RequestMapping(value = "/start", method = RequestMethod.POST)
    public AjaxData start(@Valid OperIdDto dto, BindingResult result) throws Exception {
        List<Long> operIds = SplitUtil.splitToLongList(dto.getOperId());
        return operService.start(operIds, UserContext.getUserId());
    }

    @ApiOperation(value = "禁用账号", response = AjaxData.class)
    @RequestMapping(value = "/stop", method = RequestMethod.POST)
    public AjaxData stop(@Valid OperIdDto dto, BindingResult result) throws Exception {
        List<Long> operIds = SplitUtil.splitToLongList(dto.getOperId());
        return operService.stop(operIds, UserContext.getUserId());
    }

    @ApiOperation("查询操作员角色列表")
    @ApiImplicitParam(name = "operId", value = "操作员标识", dataType = "string", paramType = "query", required = true)
    @GetMapping("queryOperRole")
    public AjaxData queryOperRole(String operId) {
        return operService.queryOperRole(Long.valueOf(operId));
    }

    @ApiOperation("保存操作员角色")
    @ApiImplicitParam(name = "model", value = "保存操作员角色参数", dataType = "SaveOperRoleModel", required = true)
    @PostMapping("saveOperRole")
    public AjaxResult saveOperRole(@RequestBody SaveOperRoleModel model) {
        return operService.saveOperRole(model.getOperId(), model.getRoleList());
    }

}
